﻿@using hc.epm.Common;
@using hc.Plat.Common.Extend;
@using hc.epm.ViewModel;
@model VisaView
@using hc.epm.Common;
@using hc.Plat.Common.Extend;
@{
    ViewBag.Title = "修改签证";
}

<body>
    <div class="contentData">
        <form id="mainForm">
            <div class="tab">
                <div class="long-text">
                    <label><span class="star">*</span>&nbsp;项目名称&nbsp;:</label>
                    <input type="text" name="ProjectName" id="ProjectName" value="@Model.ProjectName" readonly="readonly" />
                    <input type="hidden" name="ProjectId" id="ProjectId" value="@Model.ProjectId" />
                    <input type="hidden" name="Id" id="Id" value="@Model.Id" />
                    <i class="pic iconfont" width="20" height="30" style="cursor:pointer; left:596px;">&#xe76a;</i>
                </div>
                <div class="long-text">
                    <label for="VisaNo"><span class="star">*</span>&nbsp;签证类型&nbsp;:</label>
                    @Html.DropDownList("VisaType", ViewBag.VisaType as SelectList)
                    <input id="VisaTypeName" name="VisaTypeName" value="@Model.VisaTypeName" style="width: 261px;box-sizing: border-box;color: #595959;padding-left: 5px;height: 35px;line-height: 35px;margin-left: -279px;position: absolute;" type="text" />
                </div>
                <div class="long-text">
                    <label for="VisaTitle"><span class="star">*</span>&nbsp;标题&nbsp;:</label>
                    <input type="text" id="VisaTitle" name="VisaTitle" value="@Model.VisaTitle" autocomplete="off" />
                </div>
                <div class="long-text">
                    <label for="VisaNo"><span class="star"></span>&nbsp;签证号&nbsp;:</label>
                    <input type="text" id="VisaNo" name="VisaNo" value="@Model.VisaNo" autocomplete="off" />
                </div>
                <div class="intro" style="margin-top:15px;">
                    <div><label for="VisaContent"><span class="star">*</span>&nbsp;内容&nbsp;:</label></div>
                    <div>
                        <textarea class="sub-description" id="VisaContent" name="VisaContent">@Model.VisaContent</textarea>
                    </div>
                </div>
                @*<div class="long-text" style="margin-top:30px;">
                    <label for="VisaAmount"><span class="star"></span>&nbsp;金额&nbsp;:</label>
                    <input type="text" id="VisaAmount" name="VisaAmount" value="@Model.VisaAmount.ToString("0.######")" />&nbsp;&nbsp;&nbsp;万元
                </div>
                <div class="short-text">
                    <label for="VisaStartTime"><span class="star"></span>&nbsp;签证开始时间&nbsp;:</label>
                    <input type="text" id="VisaStartTime" name="VisaStartTime" onClick="WdatePicker({ el: 'VisaStartTime' })" value="@(Model.VisaStartTime == null ? "" : Convert.ToDateTime(Model.VisaStartTime).ToString("yyyy-MM-dd"))" autocomplete="off">
                    <i onClick="WdatePicker({ el: 'VisaStartTime' })" class="pic iconfont" width="20" height="30" style="cursor:pointer">&#xe7e2;</i>
                </div>
                <div class="short-text">
                    <label for="VisaEndTime"><span class="star"></span>&nbsp;签证结束时间&nbsp;:</label>
                    <input type="text" id="VisaEndTime" name="VisaEndTime" onClick="WdatePicker({ el: 'VisaEndTime' })" value="@(Model.VisaEndTime == null ? "" : Convert.ToDateTime(Model.VisaEndTime).ToString("yyyy-MM-dd"))" autocomplete="off">
                    <i onClick="WdatePicker({ el: 'VisaEndTime' })" class="pic iconfont" width="20" height="30" style="cursor:pointer">&#xe7e2;</i>
                </div>
                <div class="intro">
                    <div><label for="VisaResean"><span class="star">*</span>&nbsp;原因&nbsp;:</label></div>
                    <div>
                        <textarea class="sub-description" id="VisaResean" name="VisaResean">@Model.VisaResean</textarea>
                    </div>
                </div>*@
                <div class="upload" style="margin-bottom:0px;margin-top:20px;">
                    <div><label><span class="star">*</span>&nbsp;附件上传&nbsp;:</label></div>
                    <div class="btn-upload">
                        <input type="button" id="btnUploadFile" style="position: relative; z-index: 1;" value="上传附件" />
                        <input type="file" id="loadFile" name="loadFile" style="display:none" onchange="uploadFile()">
                        <span style="vertical-align:bottom; margin-left:15px; color:#B7B7B7;"></span>
                    </div>
                </div>
                <div style="width:100%;" id="fileListFile">
                    <ul class="fileShow"></ul>
                    <input type="hidden" name="fileDataJsonFile" id="fileDataJsonFile">
                </div>
                <div class="button-group btn-center">
                    @if (Model.State == (int)ApprovalState.Enabled)
                    {
                        <button type="button" class="saveBut" data-state="@(ApprovalState.WaitAppr.GetValue().ToString())">提交</button>
                        <button type="button" class="saveBut" data-state="@(ApprovalState.Enabled.GetValue().ToString())">保存</button>
                    }
                    else if (Model.State == (int)ApprovalState.ApprFailure)
                    {
                        <button type="button" class="saveBut" data-state="@(ApprovalState.WaitAppr.GetValue().ToString())">提交</button>
                        <button type="button" class="Discard checkright" data-module="Visa" data-right="Invalid">废弃</button>
                        <button type="button" class="Quesion">沟通</button>
                    }
                    <input type="hidden" name="State" id="State" />
                </div>
            </div>
        </form>
    </div>
    <script>
        $("#VisaType").on("change", function () {
            var visaType = $("#VisaType option:selected").text();
            $("#VisaTypeName").val(visaType);
        });

        ////金额验证（只能是正数，包括小数）
        //jQuery.validator.addMethod("Plusdigits", function (value, element, params) {
        //    var regu = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/
        //    return this.optional(element) || (regu.test(value));
        //}, "金额只能是正数");

        var fileDataArray = [];
        function uploadFile() {
            if (document.getElementById("loadFile").files.length == 0) {
            return;
        }
        var fileObj = document.getElementById("loadFile").files[0]; // js 获取文件对象
            var formFile = new FormData();
            formFile.append("file", fileObj);
            var path = formFile;

            $.ajax({
                url: "/Upload/UploadHB",
                type: "POST",
                data: path,
                contentType: false,
                processData: false,
                success: function (data) {
                    for (var i = 0; i < data.length; i++) {
                        var obj = {};
                        obj.Name = data[i].Name;
                        obj.GuidId = data[i].GuidId;
                        obj.Size = data[i].Size;
                        obj.ImageType = data[i].ImageType;
                        obj.Group = data[i].ResponseObject.FDFS_GROUP;
                        obj.Url = data[i].ResponseObject.FDFS_NAME;
                        fileDataArray.push(obj);

                        if (data[i].ImageType == null) {
                            var imgUrl = getFileImg(obj.Name);
                            var odiv = '<li id="' + obj.GuidId + '">';
                            odiv += '<div><img src="' + imgUrl + '" style="width: 20px;height: 20px;cursor: pointer;" class="imgShow"/><a href="javascript:;" target="_blank">' + obj.Name + '</a></div>';
                            odiv += '<div style="text-align:right; width:80px;">' + obj.Size + '</div>';
                            odiv += '<span style="margin-left:20px;">' + data[i].UploadName + '</span>';
                            odiv += '<span style="margin-left:20px;">' + new Date(formatDateByJson(data[i].UploadDate)).Format("yyyy-MM-dd") + '</span>';
                            odiv += '<span style="margin-left:20px;cursor: pointer;color:blue" class="fileDel" data-val="' + obj.GuidId + '">删除</span>'
                            odiv += '</li>';
                            $('#fileListFile .fileShow').append(odiv);
                        }
                    }
                    fileDataJson = JSON.stringify(fileDataArray);
                    $("#fileDataJsonFile").val(fileDataJson);
                    $("#loadFile").val("");
                }
            });
        }

        $(function () {
            $('#btnUploadFile').click(function () {
                document.getElementById("loadFile").click();
            });
            $("#mainForm").validate({
                errorClass: "validaterror",
                validClass: "validatevalid",
                errorElement: "span",
                success: function (label) {
                    label.addClass("validatevalid").html("&nbsp;");
                },
                rules: {
                    ProjectName: {
                        required: true
                    },
                    VisaNo: {
                        maxlength: 100
                    },
                    VisaTitle: {
                        required: true,
                        maxlength: 254
                    },
                    //VisaResean: {
                    //    required: true,
                    //    maxlength: 500
                    //},
                    VisaContent: {
                        required: true,
                        maxlength: 500
                    },
                    //VisaAmount: {
                    //    Plusdigits: true,
                    //    maxlength: 20
                    //},
                    //VisaStartTime: {
                    //    required: true,
                    //    date: true
                    //},
                    //VisaEndTime: {
                    //    required: true,
                    //    date: true
                    //},
                },
                messages: {
                    ProjectName: {
                        required: "项目名称不能为空"
                    },
                    VisaNo: {
                        maxlength: "签证号不能大于100个字符"
                    },
                    VisaTitle: {
                        required: "标题不能为空",
                        maxlength: "标题长度不能大于254个字符"
                    },
                    //VisaResean: {
                    //    required: "原因不能为空",
                    //    maxlength: "原因不能大于500个字符"
                    //},
                    VisaContent: {
                        required: "内容不能为空",
                        maxlength: "内容不能大于500个字符"
                    },
                    //VisaAmount: {
                    //    Plusdigits: "金额必须为数字",
                    //    maxlength: "金额不能大于20个字符"
                    //},
                    //VisaStartTime: {
                    //    required: "签证开始时间不能为空",
                    //    date: "日期格式错误"
                    //},
                    //VisaEndTime: {
                    //    required: "签证结束时间不能为空",
                    //    date: "日期格式错误"
                    //},
                }
            });
            ////判断开始时间和结束时间
            //$("#VisaStartTime,#VisaEndTime").blur(function () {
            //    var strStartTime = $("#VisaStartTime").val();
            //    var endTime = $("#VisaEndTime").val();
            //    var startNum = parseInt(strStartTime.replace(/-/g, ''), 10);
            //    var endNum = parseInt(endTime.replace(/-/g, ''), 10);
            //    if (startNum > endNum) {
            //        layer.alert("签证结束时间不能小于签证开始时间!!", { icon: 2 });
            //        $("#VisaEndTime").val("");
            //        return false;
            //    }
            //});
            //数据提交
            $(".saveBut").click(function () {
                if ($("#mainForm").valid()) {
                    if ($("#VisaTypeName").val() == "请选择") {
                        layer.alert("请选择签证类型!", { icon: 2 });
                        return false;
                    }
                    if ($(".fileShow li").length == 0) {
                        layer.alert("请先上传附件", { icon: 2 });
                        return false;
                    }
                    var state = $(this).data("state");
                    $("#State").val(state);
                    var search = $("form").serialize();
                    $.post("Edit", search, function (data) {
                        if (data.Flag) {
                            layer.msg((state == "0" ? "保存数据" : "提交数据") + "成功!", { time: 2000, icon: 1 }, function () {
                                window.location.href = "/Visa/Index";
                            });
                        } else {
                            layer.alert(data.Message, { icon: 2 });
                        }
                    });
                }
            });
        });
        layui.use(['layer', 'form', 'element', 'laypage'], function () {
            var layer = layui.layer, form = layui.form, element = layui.element, $ = layui.jquery, laypage = layui.laypage;
            //
            //获取附件
            $.post("/Files/GetFileList", { tableName: 'Epm_Visa', id: '@Model.Id' }, function (data) {
                if (data.Flag) {
                    if (data.Data.length > 0) {
                        data.Data.forEach(function (item, index, arr) {
                            if (item.ImageType == null) {
                                var odiv = '<li id="' + item.GuidId + '">';
                                odiv += '<div class="downloadFile"><img src="' + getFileImg(item.Name) + '" style="width: 20px;height: 20px;cursor: pointer;" class="imgShow"/><a href="javascript:;" target="_blank">' + item.Name + '</a></div>';
                                odiv += '<div>' + item.Size + '</div>';
                                odiv += '<span style="margin-left:20px;">' + item.CreateUserName + '</span>';
                                odiv += '<span style="margin-left:20px;">' + new Date(formatDateByJson(item.CreateTime)).Format("yyyy-MM-dd") + '</span>';
                                odiv += '<span style="margin-left:30px;cursor: pointer;color:blue" class="fileDel" data-val="' + item.GuidId + '">删除</span>';
                                odiv += '<input type="hidden" class="hiddenArea" data-group="' + item.Group + '" data-url="' + item.Url + '" data-filename="' + item.Name + '">';
                                odiv += '</li>';
                                $('#fileListFile .fileShow').append(odiv);
                            }

                            var obj = {};
                            obj.Name = item.Name;
                            obj.GuidId = item.GuidId;
                            obj.Size = item.Size;
                            obj.Group = item.Group;
                            obj.Url = item.Url;
                            obj.TableColumn = item.TableColumn;
                            obj.ImageType = item.ImageType;
                            fileDataArray.push(obj);
                        })
                        fileDataJson = JSON.stringify(fileDataArray);
                        $("#fileDataJsonFile").val(fileDataJson);
                    }
                } else {
                    parent.layer.alert(data.Message, { icon: 2 });
                }
            });

            //在建项目
            $("#ProjectName").click(function () {
                layer.open({
                    type: 2,
                    title: GetLayerTitle("选择项目"),
                    shadeClose: false, //点击遮罩关闭层
                    area: ['800px', '600px'],
                    content: '/Comm/SelectProjectAll',
                    btn: ["确定", "取消"],
                    yes: function (index, layero) {
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        var obj = iframeWin.getSelectData();
                        if (obj && obj.flag && obj.data) {
                            $("#ProjectName").val(obj.data[0].Name);
                            $("#ProjectId").val(obj.data[0].Id);
                            parent.layer.close(index);//关闭窗口
                        } else if (!obj.flag) {
                            parent.layer.msg(obj.msg, { time: 1000, icon: 2 });
                            return false;
                        }
                    },
                    btn2: function (index, layero) {
                        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                        parent.layer.close(index);//关闭窗口
                    },
                    cancel: function (index, layero) {
                    },
                });
            });

            //删除附件
            $("#fileListFile").on("click", ".fileDel", function () {
                $(this).parents("li").remove();
                var toremove = '';
                var id = $(this).data("val");
                //----删除初始文件对应的值
                fileDataArray = fileDataArray.filter(function (item, index, arr) {
                    return item.GuidId != id
                })
                fileDataJsonFile = JSON.stringify(fileDataArray);
                $("#fileDataJsonFile").val(fileDataJsonFile);
            });
            //下载附件
            $("body").on("click", ".downloadFile", function () {
                var group = $(this).parent().find(".hiddenArea").data("group");
                var path = $(this).parent().find(".hiddenArea").data("url");
                var fileName = $(this).parent().find(".hiddenArea").data("filename");
                window.location.href = "/Download/DownLoadHB?group=" + group + "&path=" + path + "&fileName=" + fileName;
            });
            //废弃
            $(".Discard").click(function () {

                var id = '@Model.Id';
                var state = "Discarded";
                if (id != "") {
                    parent.layer.confirm("确认要废弃所选择的项？", { icon: 3 }, function () {
                        $.post("UpdateState", { id: id, state: state }, function (data) {
                            if (data.Flag) {
                                parent.layer.msg("废弃成功", { time: 2000, icon: 1 });
                                window.location.href = "/Visa/Index";
                            } else {
                                parent.layer.alert(data.Message, { icon: 2 });
                            }
                        }, "json");
                    }, function () {
                        return;
                    })
                }
            });

            //沟通
            $(".Quesion").click(function () {
                var projectId = $("#ProjectId").val();
                window.location.href = "/Question/Add?businType=Visa&projectId=" + projectId + "&BusinessId=@Model.Id";
            });
        });
    </script>
</body>
